﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AMM.Business.Finance;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Finance;
using AMM.UserControls;

namespace AMM.Forms.Finance {
    public partial class FrmBankAccount : FrmBankAccountBase {

        private int _pageIndex;
        private BankAccountSearchModel _searchModel;
        private BankAccountProcessor _bankAccountProcessor;
        private List<BankAccountViewModel> _bankAccountList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1701"; }
        }

        protected override BaseSearchControl SearchControl {
            get { return new BankAccountSearchControl(); }
        }

        protected override void Add() {
            base.Add();
            this.ResetForms();
            this.ShowEditForm("新增", new BankAccount() { IsEnabled = true, ClassStatus = ClassStatus.Added });
        }

        protected override void Amend() {
            base.Amend();
            List<BankAccountViewModel> selectedList = this.dgvBankAccountList.GetSelectedRows<BankAccountViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            BankAccount bankAccount = this._bankAccountProcessor.FetchBankAccountById(selectedList[0].Key);
            bankAccount.ClassStatus = ClassStatus.Updated;
            this.ShowEditForm("修改", bankAccount);
        }

        protected override void Save() {
            base.Save();
        }

        protected override void Delete() {
            base.Delete();
            List<BankAccountViewModel> selectedList = this.dgvBankAccountList.GetSelectedRows<BankAccountViewModel>();
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._bankAccountProcessor.Delete(selectedList.Select(x => x.Key).ToArray());
                this.MDIParent.SetStatusInformation("删除成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        protected override void Search(object sender, FrmSearch.SearchArgs e) {
            this.CreateNewThread(() => {
                if (e.IsRevert) {
                    this._searchModel = new BankAccountSearchModel();
                }
                else {
                    this._searchModel = (BankAccountSearchModel)e.SearchModel;
                }
            }, () => {
                this.BindBankAccountList();
            });
        }

        #endregion

        public FrmBankAccount() {
            this.InitializeComponent();

            this.dgvBankAccountList.DataGridViewModelType = typeof(BankAccountViewModel);
        }

        #region Private methods

        private bool frmEdit_Edit(object sender, FrmEdit.EditArgs e) {
            bool resultFlag = false;
            this.PreventCancel = true;
            BankAccount bankAccount = (BankAccount)e.EditModel;
            this.CreateNewThread(() => {
                if (bankAccount.ClassStatus == ClassStatus.Added) {
                    bankAccount.CreatedBy = LoginUserName;
                    this._bankAccountProcessor.Insert(bankAccount);
                }
                else {
                    bankAccount.ModifiedBy = LoginUserName;
                    this._bankAccountProcessor.Update(bankAccount);
                }
                resultFlag = true;
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                if (resultFlag) {
                    this.MDIParent.SetStatusInformation("保存成功");
                    this.FormEdit.Close();
                }
            });
            this.Save();
            return true;
        }

        private void ResetForms() {
            this.BindBankAccountList();
        }

        private void BindBankAccountList() {
            this.CreateNewThread(() => {
                this._bankAccountList = this._bankAccountProcessor.FetchBankAccountViewModelList(this._searchModel, this._pageIndex);
            }, () => {
                this.dgvBankAccountList.DataSource = new SortableBindingList<BankAccountViewModel>(this._bankAccountList);
                this.pceList.Records = this._searchModel.Records;
                this.pceList.DataBind();
            });
        }

        private void ShowEditForm(string title, BankAccount bankAccount) {
            BankAccountEditControl editControl = new BankAccountEditControl();
            this.FormEdit = new FrmEdit();
            editControl.BaseEditModel = bankAccount;
            editControl.ValidatorForEdit = this.FormEdit.validatorForEdit;
            this.FormEdit.Controls.Add(editControl);
            this.FormEdit.Edit += new FrmEdit.EditEventHandler(frmEdit_Edit);
            this.FormEdit.Text = string.Format("{0}{1}", title, this.Text);
            FrmEntry.LoadAndRunDialog(this.FormEdit);
        }

        #endregion

        private void FrmBankAccount_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._bankAccountProcessor = new BankAccountProcessor(this.pceList.PageSize);
                this._searchModel = new BankAccountSearchModel();
                this._pageIndex = 1;
            }, () => {
                this.WindowState = FormWindowState.Maximized;
                this.BindBankAccountList();
                this.PreventCancel = false;
            });
        }

        private void pceList_Paging(object sender, PaginationArgs e) {
            this._pageIndex = e.PageIndex;
            this.BindBankAccountList();
        }

    }
}
